package com.sc.ppx_api.controller;

import com.sc.ppx_api.pojo.Content;
import com.sc.ppx_api.service.IContentService;
import com.sc.ppx_api.utils.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 帖子信息表 前端控制器
 * </p>
 *
 * @author victory
 * @since 2025-09-23 02:48:44
 */
@RestController
@RequestMapping("/content")
public class ContentController {

    @Autowired
    IContentService contentService;

    @RequestMapping("allContent")
    ResponseMsg allContent(){
        List<Content> list = contentService.list();
        return ResponseMsg.getInstance(200,list);
    }

    @RequestMapping("getById")
    ResponseMsg getById(@RequestParam(required = false) Integer content_id){
        if (null != content_id){
            Content content = contentService.getById(content_id);
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS, content);
        }
        return ResponseMsg.getInstance(ResponseMsg.ERROR, "未找到相关帖子");
    }

    //点击增加热度(+1)
    @RequestMapping("view")
    ResponseMsg increaseByView(@RequestParam(required = false) Integer content_id){
        if (null != content_id){
            Content content = contentService.getById(content_id);
            content.setHot(content.getHot()+1);
            content.setViewCount(content.getViewCount()+1);
            contentService.updateById(content);
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "热度+1");
        }
        return ResponseMsg.getInstance(ResponseMsg.ERROR, "未找到相关帖子");
    }

    //点赞增加热度(+2)
    @RequestMapping("like")
    ResponseMsg increaseByLike(@RequestParam(required = false) Integer content_id){
        if (null != content_id){
            Content content = contentService.getById(content_id);
            content.setHot(content.getHot()+2);
            content.setLikeCount(content.getLikeCount()+1);
            contentService.updateById(content);
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "热度+2");
        }
        return ResponseMsg.getInstance(ResponseMsg.ERROR, "未找到相关帖子");
    }

    //收藏增加热度(+2)
    @RequestMapping("favorite")
    ResponseMsg increaseByFavorite(@RequestParam(required = false) Integer content_id){
        if (null != content_id){
            Content content = contentService.getById(content_id);
            content.setHot(content.getHot()+2);
            content.setFavoriteCount(content.getFavoriteCount()+1);
            contentService.updateById(content);
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "热度+2");
        }
        return ResponseMsg.getInstance(ResponseMsg.ERROR, "未找到相关帖子");
    }

    //评论增加热度(+3)
    @RequestMapping("comment")
    ResponseMsg increaseByComment(@RequestParam(required = false) Integer content_id){
        if (null != content_id){
            Content content = contentService.getById(content_id);
            content.setHot(content.getHot()+3);
            content.setCommentCount(content.getCommentCount()+1);
            contentService.updateById(content);
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "热度+3");
        }
        return ResponseMsg.getInstance(ResponseMsg.ERROR, "未找到相关帖子");
    }

    //根据类别查询
    @RequestMapping("getByType")
    ResponseMsg getByType(@RequestParam(required = false) String type){
        if (null != type){
            List<Content> content = contentService.listByType(type);
            System.out.println("content:"+content);
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS, content);
        }
        return ResponseMsg.getInstance(ResponseMsg.ERROR, "未找到相关帖子");
    }

    // 取消点赞（需要添加到ContentController）
    @RequestMapping("cancelLike")
    ResponseMsg decreaseByLike(@RequestParam(required = false) Integer content_id){
        if (null != content_id){
            Content content = contentService.getById(content_id);
            // 防止出现负数
            content.setHot(Math.max(0, content.getHot() - 2));
            content.setLikeCount(Math.max(0, content.getLikeCount() - 1));
            contentService.updateById(content);
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "取消点赞成功");
        }
        return ResponseMsg.getInstance(ResponseMsg.ERROR, "未找到相关帖子");
    }
}
